export const statisticsSource = (fromPage, toPage) =>
{
    const nowTime = new Date();

    console.log(`从 ${fromPage} 页在 ${formatTime(nowTime)} 时进入详情页 ${toPage}`)
}

export const statisticsStayTime = (productId) =>
{
    const startTime = new Date(localStorage.getItem('startTime'))
    const nowTime = new Date();
    const stayTime = computeStayTime(startTime, nowTime)

    console.log(`从 ${formatTime(startTime)} 开始, 在 id为 ${productId} 的商品详情页停留了 ${stayTime} 秒`)
}

export const recordEnterPageStartTime = () =>
{
    localStorage.setItem('startTime', new Date());
}

const formatTime = (date) =>
{
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    return `${hours}:${minutes}:${seconds}`;
}

const computeStayTime = (startTime, endTime) =>
{
    const difference = endTime.getTime() - startTime.getTime()

    return Math.floor(difference / 1000)
}

